package cz.muni.fi.pa165.travelagency.services;

import cz.muni.fi.pa165.travelagency.dto.CustomerDTO;
import cz.muni.fi.pa165.travelagency.entities.Customer;
import cz.muni.fi.pa165.travelagency.persistence.ICustomerDAO;
import java.util.ArrayList;
import java.util.List;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Jan Novotný
 */
@Service
@Transactional
public class CustomerService implements ICustomerService {

    private ICustomerDAO customerDAO;

    private Mapper mapper;
    
    @Autowired
    void setCustomerDao(ICustomerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }

    @Autowired
    void setMapper(Mapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void createCustomer(CustomerDTO customer) {
        customerDAO.createCustomer(mapper.map(customer, Customer.class));
    }

    @Override
    public void deleteCustomer(CustomerDTO customer) {
        customerDAO.deleteCustomer(mapper.map(customer, Customer.class));
    }

    @Override
    public List<CustomerDTO> findAll() {
        List<Customer> customers = customerDAO.findAll();
        List<CustomerDTO> result = new ArrayList<>();
        for (Customer c : customers) {
            result.add(mapper.map(c, CustomerDTO.class));
        }
        return result;
    }

    @Override
    public List<CustomerDTO> findByName(String name) {
        List<Customer> customers = customerDAO.findByName(name);
        List<CustomerDTO> result = new ArrayList<>();
        for (Customer c : customers) {
            result.add(mapper.map(c, CustomerDTO.class));
        }
        return result;
    }

    @Override
    public CustomerDTO getCustomer(Long id) {
        Customer found = customerDAO.getCustomer(id);
        return found == null ? null : mapper.map(found, CustomerDTO.class);
    }

    @Override
    public void updateCustomer(CustomerDTO customer) {
        customerDAO.updateCustomer(mapper.map(customer, Customer.class));
    }
}
